﻿using Comm;
using Kinlo.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo
{

    public class BeforWeightFirstRep
    {
        string strConn;
       // Database DB;
        public BeforWeightFirstRep()
        {
            strConn = ConfigurationManager.ConnectionStrings["WeightDB"].ConnectionString; 
        }

        public int Add2(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var table = GlobalValues.OpType.ToUpper() == "YC" ? "BeforWeight_First" : (GlobalValues.OpType.ToUpper() == "EC" ? "BeforWeight_Second" : "BeforWeight_Thirdly");
                    var cmd =string.Format(@"INSERT dbo.{0}(Code ,BeforWeight_Index ,BeforWeight_First ,ProductDate,Weight_OK,IsValidate,[Index],OnceBeforWeight,OnceAfterWeight,LossVal)
                        VALUES(@Code, @BeforWeight_Index, @BeforWeight_First, @ProductDate,@Weight_OK,@IsValidate,@Index,@OnceBeforWeight,@OnceAfterWeight,@LossVal)", table) ;
                    var sqlCmd = new SqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_Index", w.BeforWeight_Index == null ? DBNull.Value : (object)w.BeforWeight_Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_First", w.BeforWeight_First == null ? DBNull.Value : (object)w.BeforWeight_First));
                    sqlCmd.Parameters.Add(new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new SqlParameter("@Weight_OK", w.Weight_OK == null ? DBNull.Value : (object)w.Weight_OK));
                    sqlCmd.Parameters.Add(new SqlParameter("@Index", w.Index == null ? DBNull.Value : (object)w.Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@OnceBeforWeight", w.OnceBeforWeight == null ? DBNull.Value : (object)w.OnceBeforWeight));
                    sqlCmd.Parameters.Add(new SqlParameter("@OnceAfterWeight", w.OnceAfterWeight == null ? DBNull.Value : (object)w.OnceAfterWeight));
                    sqlCmd.Parameters.Add(new SqlParameter("@LossVal", w.Index == null ? DBNull.Value : (object)w.LossVal));
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "BeforWeightFirstRep", "Add2", ex.Message)); 
                return 0;
            } 
        }
        public int Add22(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = @"INSERT dbo.BeforWeight_Second(Code ,BeforWeight_Index ,BeforWeight_First ,ProductDate,Weight_OK,IsValidate,[Index],OnceBeforWeight,OnceAfterWeight,LossVal)
                        VALUES(@Code, @BeforWeight_Index, @BeforWeight_First, @ProductDate,@Weight_OK,@IsValidate,@Index,@OnceBeforWeight,@OnceAfterWeight,@LossVal)";

                    var sqlCmd = new SqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_Index", w.BeforWeight_Index == null ? DBNull.Value : (object)w.BeforWeight_Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_First", w.BeforWeight_First == null ? DBNull.Value : (object)w.BeforWeight_First));
                    sqlCmd.Parameters.Add(new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new SqlParameter("@Weight_OK", w.Weight_OK == null ? DBNull.Value : (object)w.Weight_OK));
                    sqlCmd.Parameters.Add(new SqlParameter("@Index", w.Index == null ? DBNull.Value : (object)w.Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@OnceBeforWeight", w.OnceBeforWeight == null ? DBNull.Value : (object)w.OnceBeforWeight));
                    sqlCmd.Parameters.Add(new SqlParameter("@OnceAfterWeight", w.OnceAfterWeight == null ? DBNull.Value : (object)w.OnceAfterWeight));
                    sqlCmd.Parameters.Add(new SqlParameter("@LossVal", w.Index == null ? DBNull.Value : (object)w.LossVal));
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "BeforWeightFirstRep", "Add2", ex.Message));
                return 0;
            }
        }
         
        /// <summary>
        /// 更新状态 是否已失效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Update(string code)
        {
            var cmd = "";
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var table = GlobalValues.OpType.ToUpper() == "YC" ? "BeforWeight_First" : (GlobalValues.OpType.ToUpper() == "EC" ? "BeforWeight_Second" : "BeforWeight_Thirdly");
                    cmd = string.Format(@"UPDATE {1} SET IsValidate=0 WHERE Code='{0}'", code,table);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2} ", "BeforWeightFirstRep", "Update", ex.Message)); 
                return 0;
            }
        }
         

        public IList<BeforWeightFirst> GetBeforWeight1()
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                { 
                    var cmd = @"SELECT TOP 1000 * FROM dbo.BeforWeight_First WHERE IsValidate=1 AND	ProductDate>DATEADD(DAY,-3,GETDATE()) ORDER BY ProductDate DESC";
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var BeforWeightFirsts = sqlCmd.ExecuteReader().ReaderToList<BeforWeightFirst>();// 
                    if (BeforWeightFirsts.Count > 0)
                        return BeforWeightFirsts;
                }
                return null;

            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeight1", ex.Message)); 
                return null;
            }
        }
        /// <summary>
        /// 获取前称数据
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public BeforWeightFirst GetBeforWeightByBarcode(string barcode)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var table = GlobalValues.OpType.ToUpper() == "YC" ? "BeforWeight_First" : (GlobalValues.OpType.ToUpper() == "EC" ? "BeforWeight_Second" : "BeforWeight_Thirdly");
                    var cmd = string.Format(@"SELECT top 1 * FROM dbo.{1} WHERE  Code='{0}'  ORDER BY ProductDate DESC", barcode,table);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var BeforWeightFirsts = sqlCmd.ExecuteReader().ReaderToList<BeforWeightFirst>();
                    return BeforWeightFirsts.FirstOrDefault();
                }                 
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeightByBarcode", ex.Message));
                return null;
            }
        }

        public int DeleteBeforWeight(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var cmd = string.Format(@"DELETE dbo.BeforWeight_First WHERE Code='{0}'", w.Code);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "BeforWeightFirstRep", "DeleteBeforWeight", ex.Message)); 
                return 0;
            }
        }
    }
}
